#!/usr/bin/env python3.0

#A palindromic number reads the same both ways.
#The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
#
#Find the largest palindrome made from the product of two 3-digit numbers.

from math import floor, log

def reverse_digits(n, b=10, d=None):
    n = abs(n)
    if not d:
        d = floor(log(n, b)) + 1
    result = 0
    for i in range(d):
        (n, r) = divmod(n, b)
        result = result * b + r
    return result

def is_palindrome(n, b=10, d=None):
    return n == reverse_digits(n, b, d)


if __name__ == '__main__':
    largest_palindrome = 0
    a = 999
    while a >= 100:
        if a % 11 == 0:
            b = 999
            db = 1
        else:
            b = 990 
            db = 11
        while b >= a:
            if (a * b) <= largest_palindrome:
                break
            if is_palindrome(a*b):
                largest_palindrome = a * b
            b = b - db
        a -= 1
    print('Result: {0}'.format(largest_palindrome))


    